<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantId"
            placeholder="请输入工厂"
            clearable
            @keyup.enter="search"
            @change="closeDialog(searchParam.plantId)"
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心">
          <el-select
            v-model="searchParam.workCenterId"
            placeholder="请选择工作中心"
            :disabled="searchParam.plantId === ''"
            clearable
            @keyup.enter="search"
            filterable
          >
            <el-option
              v-for="item in workshopList"
              :key="item.id"
              :label="`${item.workshopCode}-${item.workshopName}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="供应批次">
          <el-input
            v-model="searchParam.materialBatch"
            placeholder="请输入供应批次"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="存货编码">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入存货编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="库存来源">
          <el-select
            v-model="searchParam.stockFrom"
            placeholder="请选择库存来源"
            clearable
          >
            <el-option
              v-for="item in useDictionary('StockFrom')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否正在使用">
          <el-select
            v-model="searchParam.isUse"
            placeholder="请选择是否正在使用"
            clearable
          >
            <el-option
              v-for="item in useDictionary('YesNo')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
          <el-button
            class="ml-3"
            @click="
              useDownload(
                '/mes/mesproducingstock/stockExport',
                '生产线边库存',
                searchParam,
                true,
                '.xls'
              )
            "
          >
            <template #icon><i-ep-download /></template>导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{ textAlign: 'center' }"
      >
        <el-table-column
          label="工厂"
          prop="plantCode"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="存货编码"
          prop="materialCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="批次号"
          prop="materialBatch"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="物料条码"
          prop="materialBarcode"
          min-width="250"
          show-overflow-tooltip
        />
        <el-table-column
          label="数量"
          prop="materialQuantity"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          align="left"
        >
          <template #default="scope">
            {{ useDictionary("MeasurementUnit", scope.row.quantityUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否合格"
          prop="isValid"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("YesNo", scope.row.isValid) }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否正在使用"
          prop="isUse"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("YesNo", scope.row.isUse) }}
          </template>
        </el-table-column>
        <el-table-column
          label="库存来源"
          prop="stockFrom"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("StockFrom", scope.row.stockFrom) }}
          </template>
        </el-table-column>
        <el-table-column
          label="启用状态"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="修改时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="修改人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column fixed="right" label="操作" width="210" align="center">
          <template #default="scope">
            <!--            <el-button-->
            <!--              type="primary"-->
            <!--              size="small"-->
            <!--              link-->
            <!--              @click="openDialogdetail(scope.row)"-->
            <!--            ><i-ep-search />-->
            <!--              查看-->
            <!--            </el-button>-->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <!--            <el-button-->
            <!--              type="primary"-->
            <!--              size="small"-->
            <!--              link-->
            <!--              @click="deleteItem(scope.row.id)"-->
            <!--            ><i-ep-delete />删除</el-button-->
            <!--            >-->
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesproducingstock-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESPRODUCINGSTOCK",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesproducingstock/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  plantId: "",
  stockFrom: "",
  workCenterId: "",
  materialQuantity: null,
};

const deleteParam = {
  api: "/mes/mesproducingstock/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlantList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  const { data } = await request({
    url: `/mes/mesproducingstock/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
  addDialog.visible = true;
}

/** 打开生产线边库存表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "生产线边库存详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  addDialog.visible = true;
}
/** 打开生产线边库存表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改生产线边库存数量";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增生产线边库存";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
//工厂列表
const plantList = ref([]);
const getPlantList = async () => {
  const { data } = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = data;
  searchParam.value.plantId = plantList.value[0].id;
  closeDialog(searchParam.plantId);
};
function closeDialog(plantId: String) {
  getWorkshopList(plantId);
}
//工作中心
const workshopList = ref([]);
const getWorkshopList = async (plantId: String) => {
  const { data } = await request({
    url: "mes/mesworkshop/getList",
    method: "post",
    data: { plantId: plantId },
  });
  workshopList.value = data;
};
</script>
<style lang="scss" scoped>
:deep(.el-form .el-form-item__label) {
  width: 91px;
}
</style>
